package cn.xfdzx.android.apps.shop.adapter;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kaelli.niceratingbar.NiceRatingBar;

import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.XFDGoodsDeatailsActivity;
import cn.xfdzx.android.apps.shop.fragment.home.response.HomeNearShop;
import cn.xfdzx.android.apps.shop.view.MyGridLayoutManager;
import cn.xfdzx.android.apps.shop.view.RoundRelativeLayout;

public class HomeMarketNearShopAdapter extends BaseQuickAdapter<HomeNearShop.DataBean, BaseViewHolder> {
    RoundedCorners roundedCorners;
    RequestOptions options;
    public HomeMarketNearShopAdapter() {
        super(R.layout.item_adapter_home_near_layout);
        roundedCorners = new RoundedCorners(20);
        options = RequestOptions.bitmapTransform(roundedCorners).override(200, 200);
        options.placeholder(R.drawable.icon_defult)//图片加载出来前，显示的图片
                .fallback(R.drawable.icon_defult) //url为空的时候,显示的图片
                .error(R.drawable.icon_defult);
    }

    @Override
    protected void convert(BaseViewHolder helper, HomeNearShop.DataBean item) {

        Glide.with(mContext).load(item.getLogo()).apply(options.transform(new CenterCrop(), roundedCorners)).into((ImageView) helper.getView(R.id.iv_shop_image));
        helper.setText(R.id.tv_shop_name,item.getName());
        helper.setText(R.id.tv_scroe,item.getStar());//评分
        helper.setText(R.id.tv_number_x,"月销量 "+item.getMonthly_sales());//销量

        RoundRelativeLayout layout_post = helper.getView(R.id.tv_post);
        RoundRelativeLayout layout_self = helper.getView(R.id.tv_self_store);

        if (item.getIs_self_store() == 0) {
            layout_self.setVisibility(View.GONE);
            layout_post.setVisibility(View.GONE);
        }else {
            layout_self.setVisibility(View.VISIBLE);
            layout_post.setVisibility(View.VISIBLE);
        }


        NiceRatingBar niceRatingBar = helper.getView(R.id.rb_start_level);
        niceRatingBar.setRating(Float.parseFloat(item.getStar()));
        RecyclerView recyclerView = helper.getView(R.id.recyer_goods);

        MyGridLayoutManager gridLayoutManager = new MyGridLayoutManager(mContext,3);
//        gridLayoutManager.setScrollEnabled(false);
//        recyclerView.setLayoutManager(new GridLayoutManager(mContext,3));
        recyclerView.setLayoutManager(gridLayoutManager);
        List<HomeNearShop.DataBean.GoodsBean> goods = item.getGoods();

        NearGoodChildAdapter adapter = new NearGoodChildAdapter(goods);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(mContext, XFDGoodsDeatailsActivity.class);
                intent.putExtra("good_id",  goods.get(position).getId()+"");
                mContext.startActivity(intent);

            }
        });

    }
}
